import Api_public from '../../common/api/public.js'

const state = {
	// 搜索记录
	get searchHistory(){
		return uni.getStorageSync('searchHistory') || []
	},
	set searchHistory(value){
		uni.setStorageSync('searchHistory',value)
	}
}
const mutations = {
	SET_SEARCHHISTORY(state, payload){
		let history = state.searchHistory
		if(history.indexOf(payload) == -1){
			history.unshift(payload)
		}else{
			for(let i in history){
				if(history[i] == payload){
					history.splice(i,1)
					history.unshift(payload)
				}
			}
		}
		state.searchHistory = history
	},
	SET_CLEARHISTORY(state, payload){
		state.searchHistory = []
	}
}
const actions = {
	//版本
	version({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_public.version(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//广告列表
	advertisementsList({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_public.advertisementsList(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//文章列表
	articlesList({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_public.articlesList(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//文章详情
	articlesShow({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_public.articlesShow(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//搜索作品
	worksSearch({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_public.worksSearch(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//搜索系列
	seriesSearch({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_public.seriesSearch(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//搜索作者
	authorSearch({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_public.authorSearch(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//活动详情
	activityShow({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_public.activityShow(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//拥有数量
	activityHad({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_public.activityHad(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//合成
	activityMake({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_public.activityMake(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//文章分类
	articlesType({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_public.articlesType(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//活动列表
	activityList({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_public.activityList(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//客服消息列表
	messages({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_public.messages(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//发送消息
	messagesSend({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_public.messagesSend(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
